package api_day02;

import java.util.Scanner;

/*
    String类课堂练习
 */
public class ClassWork {
    public static void main(String[] args) {
        //课堂练习1
        new Work1().work();
        //课堂练习2
        new Work2().work();
        //课堂练习3
        new Work3().work();
        //课堂练习4
        new Work4().work();
        //课堂练习5
        new Work5().work();
        //课堂练习6
        new Work6().work();
        //课堂练习7
        new Work7().work();
        //课堂练习8
        new Work8().work();
    }
}
class Work1{
    String userName;
    String password;

    public Work1() {
        userName = "张三";
        password = "123456";
    }
    public void work(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入用户名：");
        String name = sc.nextLine();
        for (int i = 3; i > 0; i--) {
            System.out.print("请输入密码：");
            String pass = sc.nextLine();
            if (pass.equals(password)){
                System.out.println("登录成功！");
                break;
            }else {
                if (i != 1) System.out.println("密码错误！还有" + (i-1) + "次机会");
                else System.out.println("登录失败！");
            }
        }
    }
}
class Work2{
    public void work(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个字符串:");
        String str = sc.nextLine();
        System.out.print("该字符串为：");
        for (int i = 0; i < str.length(); i++) {
            System.out.print(str.charAt(i) + " ");
        }
        System.out.println();
    }
}
class Work3{
    //用正则表达式做会更简单，目前没学到
    public void work(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一串字符：");
        String string = sc.nextLine();
        System.out.print("请输入要统计的字符：");
        String str = sc.nextLine();
        int cnt = 0;
        int index = string.indexOf(str);
        while (index != -1){
            cnt++;
            index = string.indexOf(str,index+str.length());
        }
        System.out.println("指定字符字串出现" + cnt + "次");
        /* 原思路，上面是优化后
        if (string.contains(str)){
            int cnt = 1;
            int index = string.indexOf(str);
            while (index != string.length()-1){
                if (string.indexOf(str,index+str.length()) != -1){
                    index = string.indexOf(str,index+str.length());
                    cnt += 1;
                }else index = string.length()-1;
            }
            System.out.println("指定字符字串出现" + cnt + "次");
        }else {
            System.out.println("不包含指定子串");
        }
        */
    }
}
class Work4{
    public void work(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一串密码：");
        String password = sc.nextLine();
        //可以优化，for循环遍历一次，然后计数，再进行条件判断
        //另一种遍历思路，先用toCharArray()转成字符数组再比较
        do {
            //布尔型标记符用于判断是否出循环
            boolean flag = true;
            //字符数判断
            if (password.length() < 9) {
                System.out.println("请输入至少九个字符的密码");
                flag = false;
            }
            //大写字符数判断
            int cnt_big = 0;
            for (int i = 0; i < password.length(); i++) {
                char c = password.charAt(i);
                if (c > 64 & c < 91) cnt_big += 1;
            }
            if (cnt_big < 2) {
                System.out.println("必须包含至少2个大写字符");
                flag = false;
            }
            //小写字母和数字判断
            int cnt_small = 0,cnt_number = 0;
            for (int i = 0; i < password.length(); i++) {
                char c = password.charAt(i);
                if (c > 96 & c < 123) cnt_small += 1;
                if (c > 47 & c < 58) cnt_number += 1;
            }
            if (cnt_small == 0 | cnt_number == 0) {
                System.out.println("必须包含小写字母和数字");
                flag = false;
            }
            if (!flag){
                System.out.print("请重新输入：");
                password = sc.nextLine();
            }else break;
        } while (true);
        System.out.println("密码合法！");
    }
}
class Work5{
    public void work(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入手机号：");
        String phone_number = sc.nextLine();
        //如果中间四位与后四位相同，则会变成111******** -> 待解决
        String mid = phone_number.substring(3,7);
        System.out.println(phone_number.replace(mid,"****"));
    }
}
class Work6{
    public void work(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入学生信息（以逗号相隔）：");
        String info = sc.nextLine();
        Student student = new Student(info.substring(0,2),Integer.parseInt(info.substring(3)));
        System.out.println(student);
    }
}
class Work7{
    int[] arr = {1,2,3};
    public void work(){
        System.out.print("[");
        for (int i = 0; i < arr.length - 1 ; i++) {
            System.out.print(arr[i] + "，");
        }
        System.out.println(arr[arr.length - 1] + "]");
    }
}
class Work8{
    public void work(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个字符串：");
        String input = sc.nextLine();
        for (int i = input.length(); i > 0; i--) {
            System.out.print(input.charAt(i-1));
        }
    }
}